/**
 * MenuItem 组件
 */
import React, { useContext, FC, CSSProperties } from 'react';
import classNames from 'classnames';
import { MenuContext } from './menu';

export interface IMenuItemProps {
  index?: string; // 高亮
  disabled?: boolean;
  className?: string;
  style?: CSSProperties;
}

export const MenuItem: FC<IMenuItemProps> = (props) => {
  const {
    index,
    disabled,
    className,
    style,
    children,
    ...rest
  } = props;

  const context = useContext(MenuContext);

  const classes = classNames('menu-item', className, {
    'is-disabled': disabled,
    'is-active': context.index === index,
  });

  const handleClick = () => {
    if(context.onSelect && !disabled && (typeof index === 'string')) {
      context.onSelect(index);
    } 
  }

  return (
    <li
      className={classes}
      style={style}
      onClick={handleClick}
      {...rest}
    >
      {children}
    </li>
  );
};

MenuItem.displayName = 'MenuItem';

export default MenuItem;